package mortal.common.sound
{
	import com.gengine.media.core.play.*;
	import com.gengine.media.events.*;
	import com.greensock.*;
	import flash.events.*;
	import flash.system.*;
	import flash.utils.*;
	import mortal.game.resource.*;
	
	public class SoundManager extends Object
	{
		private var _musicVolume:Number = 0.5;
		private var _effectSoundVolune:Number = 0.5;
		private var _soundLoaded:Dictionary;
		private var _soundPlaying:Dictionary;
		private var _backGroundMusic:MediaPlayCore;
		private var _timer:Timer;
		private var _fadingStart:Boolean = false;
		private var _fadingStop:Boolean = false;
		private static var _instance:SoundManager = null;
		
		public function SoundManager()
		{
			this._soundLoaded = new Dictionary();
			this._soundPlaying = new Dictionary();
			this._timer = new Timer(100);
			if (_instance)
			{
				throw new Error("SoundManager 单例！");
			}
			
		}
		
		public function setBackGroundMusic(param1:int, param2:String = "") : void
		{
			var _loc_4:Object = null;
			var _loc_5:MediaPlayCore = null;
			var _loc_6:String = null;
			if (!Capabilities.hasAudio)
			{
				trace("系统没有音频功能！");
				
			}
			var _loc_3:* = MusicsAndSoundsConfig.instance.getMusicFileNameByMapId(param1, param2);
			if (_loc_3 && this._soundLoaded[_loc_3])
			{
				if (this._backGroundMusic && this._backGroundMusic.url == (this._soundLoaded[_loc_3] as MediaPlayCore).url)
				{
					if (this._backGroundMusic.stopped)
					{
						this.fadeStart(this._backGroundMusic, this.musicVolume);
					}
					
				}
				else if (this._backGroundMusic && !this._backGroundMusic.stopped)
				{
					this.fadeStop(this._backGroundMusic);
				}
				this._backGroundMusic = this._soundLoaded[_loc_3] as MediaPlayCore;
				if (!this._backGroundMusic.stopped)
				{
					this._backGroundMusic.stop();
				}
				this.fadeStart(this._backGroundMusic, this.musicVolume);
			}
			else
			{
				if (this._backGroundMusic && !this._backGroundMusic.stopped)
				{
					this.fadeStop(this._backGroundMusic);
				}
				_loc_4 = {onMediaEvent:this.onMediaEventHandler};
				_loc_6 = this.getMediaType(_loc_3);
				switch(_loc_6)
				{
					case "mp3":
					{
						_loc_5 = new MP3PlayCore(this.getUrlByName(_loc_3), 1000, false, 250, _loc_4);
						break;
					}
					case "flv":
					case "f4v":
					case "mp4":
					case "m4a":
					case "mov":
					case "mp4v":
					case "aac":
					case "aa":
					case "3gp":
					case "3g2":
					{
						_loc_5 = new NetStreamPlayCore(this.getUrlByName(_loc_3), 1000, true, 250, _loc_4);
						break;
					}
					default:
					{
						trace("暂不支持该媒体文件格式或文件格式出错！");
						
						break;
					}
				}
				this.fadeStart(_loc_5, this.musicVolume);
				this._backGroundMusic = _loc_5;
				this._soundLoaded[_loc_3] = _loc_5;
			}
			
		}
		
		private function stopBackgroundMusic() : void
		{
			if (this._backGroundMusic && !this._backGroundMusic.stopped)
			{
				this.fadeStop(this._backGroundMusic);
			}
			
		}
		
		private function startBackgroundMusic() : void
		{
			if (this._backGroundMusic && this._backGroundMusic.stopped)
			{
				this.fadeStart(this._backGroundMusic, this.musicVolume);
			}
			
		}
		
		private function onMediaEventHandler(event:MediaPlayEvent) : void
		{
			var _loc_2:* = event.type;
			switch(_loc_2)
			{
				case MediaPlayEvent.PLAY_COMPLETE:
				{
					this._timer.addEventListener(TimerEvent.TIMER, this.onTimerComplete);
					this._timer.start();
					break;
				}
				default:
				{
					break;
				}
			}
			
		}
		
		private function onTimerComplete(event:TimerEvent) : void
		{
			this._timer.removeEventListener(TimerEvent.TIMER, this.onTimerComplete);
			this._timer.stop();
			this._timer.reset();
			this.fadeStart(this._backGroundMusic, this.musicVolume);
			
		}
		
		private function getUrlByName(param1:String) : String
		{
			var _loc_2:String = null;
			var _loc_3:int = 0;
			if (param1)
			{
				_loc_2 = ResConfig.instance.getUrlByName(param1);
				_loc_3 = _loc_2.indexOf("?v=");
				if (_loc_3 != -1)
				{
					_loc_2 = _loc_2.substring(0, _loc_3);
				}
				return _loc_2;
			}
			return "";
		}
		
		public function soundPlay(param1:String) : void
		{
			var _loc_3:MediaPlayCore = null;
			if (!Capabilities.hasAudio)
			{
				trace("系统没有音频功能！");
				
			}
			var _loc_2:* = MusicsAndSoundsConfig.instance.getSoundFileNameByType(param1);
			param1 = this.getMediaType(_loc_2);
			switch(param1)
			{
				case "mp3":
				{
					_loc_3 = new MP3PlayCore(this.getUrlByName(_loc_2), 1000, false, 250, {onMediaEvent:this.onSoundCompleteHandler});
					break;
				}
				case "flv":
				case "f4v":
				case "mp4":
				case "m4a":
				case "mov":
				case "mp4v":
				case "aac":
				case "aa":
				case "3gp":
				case "3g2":
				{
					_loc_3 = new NetStreamPlayCore(this.getUrlByName(_loc_2), 1000, false, 250, {onMediaEvent:this.onSoundCompleteHandler});
					break;
				}
				default:
				{
					trace("暂不支持该媒体文件格式！");
					
					break;
				}
			}
			_loc_3.volume = this.effectSoundVolune;
			_loc_3.play();
			
		}
		
		private function onSoundCompleteHandler(event:MediaPlayEvent) : void
		{
			var _loc_2:* = event.type;
			switch(_loc_2)
			{
				case MediaPlayEvent.PLAY_COMPLETE:
				{
					event.target.dispose();
					break;
				}
				default:
				{
					break;
				}
			}
			
		}
		
		private function getMediaType(param1:String = null) : String
		{
			var _loc_2:Array = null;
			var _loc_3:int = 0;
			var _loc_4:String = null;
			if (param1)
			{
				_loc_2 = param1.split(".");
				_loc_3 = _loc_2.length;
				_loc_4 = _loc_2[(_loc_3 - 1)];
				return _loc_4.toLowerCase();
			}
			return "";
		}
		
		private function fadeStart(param1:MediaPlayCore, param2:Number) : void
		{
			param1.volume = 0;
			param1.play();
			this._fadingStart = true;
			TweenLite.to(param1, 3, {volume:param2, onComplete:this.onCompleteHandler});
			
		}
		
		private function onCompleteHandler() : void
		{
			this._fadingStart = false;
			
		}
		
		private function fadeStop(param1:MediaPlayCore) : void
		{
			var sound:* = param1;
			var onCompleteHandler:* = function () : void
			{
				if (!sound.stopped)
				{
					sound.stop();
				}
				_fadingStop = false;
				
			}
				;
			this._fadingStop = true;
			TweenLite.to(sound, 2, {volume:0, onComplete:onCompleteHandler});
			
		}
		
		public function get musicVolume() : Number
		{
			return this._musicVolume;
		}
		
		public function set musicVolume(param1:Number) : void
		{
			this._musicVolume = param1;
			if (this._backGroundMusic)
			{
				if (this._fadingStart)
				{
					TweenLite.killTweensOf(this._backGroundMusic);
				}
				this._backGroundMusic.volume = param1;
			}
			
		}
		
		public function get effectSoundVolune() : Number
		{
			return this._effectSoundVolune;
		}
		
		public function set effectSoundVolune(param1:Number) : void
		{
			this._effectSoundVolune = param1;
			
		}
		
		public function get backGroundMusic() : MediaPlayCore
		{
			return this._backGroundMusic;
		}
		
		public function set backGroundMusic(param1:MediaPlayCore) : void
		{
			this._backGroundMusic = param1;
			
		}
		
		public static function get instance() : SoundManager
		{
			if (!_instance)
			{
				_instance = new SoundManager;
			}
			return _instance;
		}
		
	}
}
